//index.js
//获取应用实例
const app = getApp()

function countdown(that) {

  var EndTime = that.data.end_time || [];
  var NowTime = new Date().getTime();
  var total_micro_second = EndTime - NowTime || [];
  console.log('剩余时间：' + total_micro_second);
  // 渲染倒计时时钟
  that.setData({
    clock: (parseInt(total_micro_second / 1000) + 1).toString() + "s后进入"
  });
  if (total_micro_second <= 0) {
    that.setData({
      clock: "正在跳转"
    });
    app.globalData.splash_current = 1;
    // that.bindToIndex();
    that.bindToLocal();
    return;
  }
  setTimeout(function () {
    total_micro_second -= 1000;
    countdown(that);
  }
    , 1000)
}

// 时间格式化输出，如11:03 25:19 每1s都会调用一次
function dateformat(micro_second) {
  // 总秒数
  var second = Math.floor(micro_second / 1000);
  // 天数
  var day = Math.floor(second / 3600 / 24);
  // 小时
  var hr = Math.floor(second / 3600 % 24);
  // 分钟
  var min = Math.floor(second / 60 % 60);
  // 秒
  var sec = Math.floor(second % 60);
  return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";
}

Page({
  data: {
    nullHouse: true, //弹窗默认隐藏
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    register:false,
    userType:0,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    splash_url:null,
    end_time: '',
    clock: '',
    count_down:2,
  },
  //事件处理函数
  bindToLogin: function () {
    wx.redirectTo({
      url: '../login/login'
    })
  },
  bindToAgentRegister: function () {
    console.log("欢迎注册")
    app.globalData.userType = 20
    wx.redirectTo({
      url: '../register/register'
    })
  },  
  bindToUserRegister: function () {
    console.log("欢迎注册")
    app.globalData.userType = 10
    wx.redirectTo({
      url: '../register/register'
    })
  },    
  bindToLocal: function () {
    wx.redirectTo({
      url: '../local/local'
    })
  },  
  bindViewTap: function() {
    wx.redirectTo({
      url: '../logs/logs'
    })
  },
  bindToIndex: function () {
    wx.redirectTo({
      url: '../index/index'
    })
  },
  bindRegister:function(){
    console.log("欢迎注册")
  },
  onLoad: function () {
    wx.setNavigationBarTitle({ title: '欢迎使用粉丝保' })

    var that = this;
    var splash_url =  app.globalData.base + app.globalData.splash_url
    console.log(splash_url)
    that.setData({
      splash_url: splash_url,
      end_time : new Date().getTime() + that.data.count_down * 1000,
    })
    countdown(that);
  },
})
